# # 启动项目后开启定时工作.每天检测商品上下架时间
# from apscheduler.schedulers.background import BackgroundScheduler
# from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
# try:
#     # 实例化调度器
#     scheduler = BackgroundScheduler()
#     # 调度器使用DjangoJobStore()
#     scheduler.add_jobstore(DjangoJobStore(), "default")
#     # 设置定时任务，另一种方式为方式为interval，时间间隔为10s
#     # 每天固定时间执行任务，对应代码为：
#     scan_time = "商品上下架扫描时间"+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#     @register_job(scheduler, 'cron', day_of_week='mon-fri', hour='00', minute='00', second='00',id=scan_time)
#     # @register_job(scheduler, "interval", seconds=10)
#     def modify_goods_status():
#         print('定时任务开启',datetime.datetime.now())
#         today = datetime.date.today()
#         from goods.models import Goods
#         # 获取当天要上架的商品
#         goods_list = Goods.objects.filter(start_time=today)
#         for goods in goods_list:
#             if goods.end_time <= datetime.date.today(): continue
#             goods.goods_status = 1
#             goods.save()
#         # 获取当天要下架的商品
#         Goods.objects.filter(end_time=today).update(goods_status=0)
#     register_events(scheduler)
#     scheduler.start()
# except Exception as e:
#     print(e)
#     # 有错误就停止定时器
#     # scheduler.shutdown()

